<template>
  <div class="main_box">
    <el-row class="header-row">
      <el-col>
        <div class="search_wrap">
          <el-form :inline="true" :model="filters" class="demo-form-inline" ref="searchForm">
            <el-form-item prop="itemName">
              <el-input v-model="filters.itemName" placeholder="项目名称"></el-input>
            </el-form-item>
            <el-form-item prop="itemCode">
              <el-input v-model="filters.itemCode" placeholder="编号"></el-input>
            </el-form-item>
            <el-form-item prop="institutionId">
              <el-select v-model="filters.institutionId" :clearable="true" filterable :filter-method="listInstitution" placeholder="请选择医检机构">
                <el-option v-for="item in pickInstitutionList" :key="item.value" :label="item.label" :value="item.value"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item prop="insItemCode">
              <el-input v-model="filters.insItemCode" placeholder="医检编号"></el-input>
            </el-form-item>
            <!--<el-form-item prop="appointState">
              <el-select v-model="filters.appointState" :clearable="true" placeholder="请选择报告单状态">
                <el-option
                  v-for="item in reviewStatue"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item prop="date">
              <el-date-picker v-model="filters.date" :clearable="true" type="datetimerange" range-separator="至" start-placeholder="送检开始日期" end-placeholder="送检结束日期"></el-date-picker>
            </el-form-item>-->
            <el-form-item>
              <el-button icon="el-icon-search" type="primary" @click="listAppoint">查询</el-button>
              <el-button icon="el-icon-refresh-left" @click="resetForm">重置</el-button>
            </el-form-item>
          </el-form>
        </div>
      </el-col>
      <el-col>
        <div class="opration_wrap">
          <el-tooltip content="刷新" placement="top">
            <el-button circle icon="el-icon-refresh-right" @click="reload" size="mini"></el-button>
          </el-tooltip>
          <el-button type="primary" plain icon="fa fa-plus" v-has="permissionAdd" @click="handleClickAdd()" size="mini"> 新增</el-button>
        </div>
      </el-col>
    </el-row>
    <!--<el-row>
      <div style="margin-left: 10px;margin-bottom: 10px;margin-top: 10px;" >
        <span>勾选批量操作：</span><el-button size="small" type="success" v-has="permissionBatchPrint" @click="handleClickBatchPrint" >批量打印</el-button>
      </div>
    </el-row>-->

    <el-row ref="tableRow">
      <!-- 列表 -->
      <el-table :data="appointList" :fit="true" highlight-current-row>
        <el-table-column type="index" label="序号" width="50" align="center"></el-table-column>
        <el-table-column prop="itemCode" align="center" label="编号" width="70"></el-table-column>
        <el-table-column prop="itemName" align="center" label="项目名称" width="200"></el-table-column>
        <el-table-column label="分类" width="200">
          <template slot-scope="scope">
            <span>{{ scope.row.firstClassifyName }}</span> -
            <span>{{ scope.row.classifyName }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="institutionName" label="医检机构" width="200"></el-table-column>
        <el-table-column prop="insOfficeName" label="科室" align="center" width="100"></el-table-column>
        <el-table-column prop="insItemCode" label="医检编号" align="center" width="100"></el-table-column>
        <el-table-column prop="itemTime" label="时长" align="center" width="100"></el-table-column>
        <el-table-column prop="itemPrice" label="收费价格" align="center" width="100"></el-table-column>
        <el-table-column label="状态" align="center" width="100">
          <template slot-scope="scope">
            <span>
              <el-tag size="mini" :type="formatStateType(scope.row.state)" effect="dark">
                {{ formatState(scope.row.state) }}
              </el-tag>
            </span>
          </template>
        </el-table-column>
        <el-table-column prop="createDate" label="创建时间"></el-table-column>
        <el-table-column label="操作" align="center" width="300">
          <template slot-scope="scope">
            <el-button v-has="permissionCheck" @click="handleClickDetail(scope.row)" icon="el-icon-view" size="mini">查看</el-button>
            <el-button v-has="permissionEdit" @click="handleClickEditor(scope.row)" icon="el-icon-edit-outline" size="mini">编辑</el-button>
            <el-button v-has="permissionUpStatus" @click="handleIsDisable(scope.row)" icon="el-icon-delete" size="mini">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页工具条 -->
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page.pageNo"
        :page-sizes="[20, 50, 100]"
        :page-size="page.pageSize"
        layout="total, sizes, jumper, next, pager, prev"
        :total="page.pageTotal"
      ></el-pagination>
    </el-row>
  </div>
</template>
<script>
import * as message from '@/constants/message';
import { FORMAT_DATE_YYYY_MM_DD } from '@/constants/constants';
import AppointItemApi from '@/api/appoint/appointItem';
import InstitutionApi from '@/api/institution/institution';

export default {
  name: 'AppointItemManagement',
  inject: ['reload'], //注入局部刷新
  data() {
    return {
      appointList: [],

      filters: {
        institutionId: '',
        itemCode: '',
        insItemCode: '',
        itemName: '',
      },
      page: {
        pageNo: 1,
        pageTotal: 0,
        pageSize: 20,
      },
      pickInstitutionList: [],
      reviewStatue: [
        { label: '检测中', value: 0 },
        { label: '已出结果', value: 1 },
      ],
      agenciesStatue: [
        { label: '未打印', value: 0 },
        { label: '已打印', value: 1 },
      ],

      permissionAdd: 'SYSTEM_MANAGER_APPOINT_ITEM_LIST_ADD',
      permissionCheck: 'SYSTEM_MANAGER_APPOINT_ITEM_LIST_SEE',
      permissionEdit: 'SYSTEM_MANAGER_APPOINT_ITEM_LIST_EDIT',
      permissionUpStatus: 'SYSTEM_MANAGER_APPOINT_ITEM_LIST_DELETE',
      excelData: {},
      allList: [],
    };
  },
  components: {},
  created() {
    window.addEventListener('keydown', this.keyDown);
  },
  mounted() {
    this.init();
  },
  activated() {
    this.listAppoint();
    console.log('初始化123456');
    // if (this.$route.meta.keepAlive) {
    // }
  },
  destroyed() {
    window.removeEventListener('keydown', this.keyDown, false);
  },
  methods: {
    init() {
      let _this = this;
      _this.initDate();
      _this.listInstitution();
      _this.listAppoint();
      _this.getTableHeight();
    },
    keyDown(e) {
      //如果是回车则执行查询方法
      if (e.keyCode == 13) {
        this.listAppoint();
      }
    },


    initDate() {
      let begin = new Date();
      begin.setHours(0);
      begin.setMinutes(0);
      begin.setSeconds(0);
      begin.setMonth(begin.getMonth() - 1);
      let end = new Date();
      end.setHours(23);
      end.setMinutes(59);
      end.setSeconds(59);
      this.filters.date = [begin, end];
    },
    resetForm() {
      this.$refs.searchForm.resetFields();
      this.page.pageNo = 1;
      this.listAppoint();
    },
    // 当过滤器放在第三方组件下 若 row, column, 会报错
    formatState(cellValue) {
      if (cellValue == -1) {
        return '删除';
      } else if (cellValue == 0) {
        return '禁用';
      } else if (cellValue == 1) {
        return '启用';
      } else if (cellValue == 2) {
        return '待审核';
      }
    },
    formatStateType(cellValue) {
      if (cellValue == -1) {
        return 'danger';
      } else if (cellValue == 0) {
        return 'info';
      } else if (cellValue == 1) {
        return 'success';
      } else if (cellValue == 2) {
        return '';
      }
    },
    formatDate(row, column, cellValue) {
      let _this = this;
      return _this.$Utils.formatDate(cellValue, FORMAT_DATE_YYYY_MM_DD);
    },
    formatDateTime(row, column, cellValue) {
      let _this = this;
      return _this.$Utils.formatDate(cellValue, 'yyyy-MM-dd hh:mm:ss');
    },
    listInstitution() {
      let _this = this;
      _this.loadingIns = true;
      let cond = {
        institutionType: 1,
      };
      InstitutionApi.getInstitutionNameList(cond)
        .then((res) => {
          setTimeout(() => {
            _this.loadingIns = false;
            _this.pickInstitutionList = res.data.map((item) => {
              return {
                value: item.institutionId,
                label: item.institutionName,
              };
            });
          }, 200);
        })
        .catch((error) => {
          _this.pickInstitutionList = [];
          console.error(error);
        });
    },
    handleSizeChange(size) {
      let _this = this;
      _this.page.pageSize = size;
      _this.listAppoint();
    },
    handleCurrentChange(val) {
      let _this = this;
      _this.page.pageNo = val;
      _this.listAppoint();
    },
    handleClickEditor(row) {
      let _this = this;
      _this.$router.push({
        path: '/appoint/itemEdit',
        query: { id: row.itemId },
      });
    },
    handleIsDisable(row) {
      let _this = this;
      this.$confirm('是否删除该项目', message.TITLE_PROMPT, {}).then(() => {
        AppointItemApi.deleteAppointItem({
          itemId: row.itemId,
          state: -1,
        }).then((res) => {
          _this.listAppoint();
        });
      });
    },
    handleClickDelete(val) {
      let _this = this;
      _this.$confirm(message.MSG_CONFIRM_DELETE, message.TITLE_PROMPT, {}).then(() => {
        AppointItemApi.deleteAppointItem({
          appointId: val.appointId,
          state: -1,
        }).then((res) => {
          _this.listAppoint();
        });
      });
    },
    handleClickAdd() {
      let _this = this;
      _this.$router.push('/appoint/itemAdd');
    },
    handleClickDetail(row) {
      let _this = this;
      _this.$router.push({
        path: '/appoint/itemDetail',
        query: { id: row.itemId },
      });
    },
    listAppoint() {
      let _this = this;
      let cond = {
        pageNo: _this.page.pageNo,
        pageSize: _this.page.pageSize,
        itemName: _this.filters.itemName,
        itemCode: _this.filters.itemCode,
        classifyId: _this.filters.classifyId,
        insItemCode: _this.filters.insItemCode,
        institutionId: _this.filters.institutionId,
      };
      AppointItemApi.listAppointItem(cond).then((res) => {
        _this.appointList = res.data.list;
        _this.page.pageTotal = res.data.total;
      });
    },
  },
};
</script>
<style lang="scss" src="@/assets/css/views/content.scss" scope></style>
